<template>
    <div class="container">
        <div class="title">
            <span>修改</span>
            <span class="back" @click="back">
        <i class="iconfont icon-fanhui"></i> 返回
      </span>
        </div>
        <el-divider></el-divider>
        <br/>
        <el-form label-width="120px"
                 :model="form"
                 status-icon
                 ref="form"
                 @submit.native.prevent>
            <el-form-item label="图片" prop="img_id">
                <upload-imgs
                        ref="uploadEle"
                        :value="imgValue"
                        :rules="rules"
                        :multiple="true"
                        :min-num="1"
                        :max-num="1"
                />
                <div>推荐上传  </div>
            </el-form-item>
            <el-form-item label="分类" prop="sort_id">
                <el-select v-model="form.sort_id" filterable placeholder="请选择">
                    <el-option
                            v-for="item in sortArr"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="名称" prop="name">
                <el-input size="medium" v-model="form.name" placeholder="请填写名称"></el-input>
            </el-form-item>
            <el-form-item label="价格" prop="price">
                <el-input size="medium" type="number" v-model="form.price" placeholder="请填写价格"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="order_sort">
                <el-input size="medium" type="number" v-model="form.order_sort" placeholder="请填写排序(值越大越在前)"></el-input>
            </el-form-item>
            <el-form-item class="submit">
                <el-button type="primary" @click="submitForm('form')">保 存</el-button>
                <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import price from '@/models/price'
import UploadImgs from '@/components/base/upload-imgs'

export default {
  components: {
    UploadImgs,
  },
  props: {
    priceId: {
      type: Number,
    },
  },
  data() {
    return {
      sortArr: [],
      imgValue: [],
      form: {
        sort_id: '',
        name: '',
        img_id: '',
        order_sort: '',
      },
      rules: {
        maxSize: 0.2,
      },
    }
  },
  mounted() {
    this.getInfo()
    this.getSortAll()
  },
  methods: {
    async getSortAll() {
      this.sortArr = await price.getPriceSortAll()
    },
    // 获取详情
    async getInfo() {
      const info = await price.getPriceInfo(this.priceId)
      this.imgValue = [{
        delete: false,
        id: info.id,
        imgId: info.img_id,
        display: info.img.url,
      }]
      this.form = {
        sort_id: info.sort_id,
        name: info.name,
        img_id: info.img_id,
        price: info.price,
        order_sort: info.order_sort,
      }
    },
    // eslint-disable-next-line no-unused-vars
    async submitForm(formName) {
      const self = this
      const imgValue = await self.$refs.uploadEle.getValue()
      // eslint-disable-next-line no-unused-vars
      const info = {
        id: this.priceId,
        sort_id: self.form.sort_id,
        img_id: Number(imgValue[0].imgId),
        name: self.form.name,
        price: self.form.price,
        order_sort: self.form.order_sort,
      }
      try {
        // eslint-disable-next-line no-unused-vars
        const res = await price.updatePrice(info)
        if (res.error_code === 0) {
          this.$message.success(`${res.msg}`)
          this.back()
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 重置表单
    resetForm() {
      this.getInfo()
    },
    back() {
      this.$emit('editClose')
    },
  },
}
</script>

<style lang="scss" scoped>
    .el-divider--horizontal {
        margin: 0
    }

    .container {
        .title {
            height: 59px;
            line-height: 59px;
            color: $parent-title-color;
            font-size: 16px;
            font-weight: 500;
            text-indent: 40px;

            .back {
                float: right;
                margin-right: 40px;
                cursor: pointer;
            }
        }

        .wrap {
            padding: 20px;
        }

        .submit {
            float: left;
        }
    }
</style>
